using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using User.Models;
using UserSorce.Entity.Models;

namespace UserLogicCore.Logic
{
    public class UserService
    {
        private ToDoListWebContext _ctx = new ToDoListWebContext();

        //用户登录
        public int FindUser(string name, string password)
        {
            var user = _ctx.Users.SingleOrDefault(prop => prop.UserName.Equals(name) && prop.Password.Equals(password));
            if (user != null)
                return user.UserId;
            else
                return 0;
        }
        //用户注册
        public bool EnrollUser(string name, string password, string nickName)
        {
            if (_ctx.Users.SingleOrDefault(prop => prop.UserName.Equals(name)) == null)
            {
                Users user = new Users()
                {
                    Password = password,
                    UserName = name,
                    NickName = nickName,
                    RegistTime = DateTime.Now,
                };
                _ctx.Add(user);
                _ctx.SaveChanges();
                return true;
            }
            else
                return false;
        }
        //注销用户
        public void DeleteUser(int id)
        {
            _ctx.Remove(_ctx.Users.SingleOrDefault(prop => prop.UserId == id));
            _ctx.SaveChanges();
        }
        //得到用户所有的信息
        public Users GetData(int userId)
        {
            return _ctx.Users.Include("Inventory").Include("Type").Include("Matter").SingleOrDefault(prop => prop.UserId == userId);
        }
    }
}
